<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use think\Db;
use app\vehicle\model\User;
use app\vehicle\model\UserInfo;
use app\vehicle\model\UserCredit;
class certificationLogic
{
    public function getCertification(){

    }
    public function subCertification($data,$user){
        Db::startTrans();
        try {
            $check = UserInfo::where('business_number','eq',$data['license'])->count();
            if($check){
                return '该单位资料已上传';
            }
            if($user['status']>0){
                return '审核中';
            }
            $info['user_id'] = $user['id'];
            $info['company'] = $data['company'];
            $info['business_licence'] = $data['images'][0];
            $info['business_number'] = $data['license'];
            $info['person_name'] = $data['person'];
            $info['person_id_card'] = $data['certificate'];
            $info['person_id_card_front'] = $data['images'][1];
            $info['person_id_card_back'] = $data['images'][2];
            $info['manage_name'] = $data['name'];
            $info['manage_mobile'] = $data['mobile'];
            $info['manage_id_card_front'] = $data['images'][3];
            $info['manage_id_card_back'] = $data['images'][4];
            $business_id = UserInfo::insertGetId($info);
            $user_data['status'] = 1;
            $user_data['business_name'] = $data['company'];
            $user_data['business_id'] = $business_id;
            User::where('id','eq',$user['id'])->update($user_data);
            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
    }
    public function getCertificationInfo($user){
        $result = User::with('info')->where('id','eq',$user['id'])->field('id,business_name')->find();
        return $result;
    }
    //信贷资料上传
    public function subCredit($data,$user){
        Db::startTrans();
        try {
            if($user['credit_status']>0){
                return '审核中';
            }
            $user_data['credit_status'] = 1;
            User::where('id','eq',$user['id'])->update($user_data);
            $info['user_id'] = $user['id'];
            $info['unit_name'] = $data['info'][0];
            $info['business_number'] = $data['info'][1];
            $info['person_name'] = $data['info'][2];
            $info['open_name'] = $data['info'][3];
            $info['open_bank'] = $data['info'][4];
            $info['mobile'] = $data['info'][5];
            $info['business_licence'] = $data['uploadImg'][0];
            $info['license_img'] = $data['uploadImg'][1];
            $info['person_positive'] = $data['uploadImg'][2];
            $info['person_against'] = $data['uploadImg'][3];
            UserCredit::insert($info);
            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
    }
    public function getCreditInfo($user){
        $result = UserCredit::where('user_id','eq',$user['id'])->find();
        return $result;
    }
}